package com.phone;

import com.phone.entity.APP;
import com.phone.entity.CashHistory;
import com.phone.entity.PointHistory;
import com.phone.entity.User;
import com.phone.exception.ErrorException;
import com.phone.exception.ParameterException;
import com.phone.handle.PointHandle;
import com.phone.json.JsonControl;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by tisen on 2017/8/9.
 */
public class Test {

    private static final String NAME = "name";
    private static final String INTRODUCTION = "introduction";
    private static final String ICON_URL = "icon_url";
    private static final String FILE_URL = "file_url";
    private static final String VERSION = "version";

    public static void main(String[] args) {
        try {
            test4();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    private static void test4() throws UnsupportedEncodingException {

        System.out.println(getAPIString(new APP(
                "WeChat",
                "一款跨平台的通讯工具。支持单人、多人参与。通过手机网络发送语音、图片、视频和文字",
                "http://oud8gdknf.bkt.clouddn.com/wx_icon.jpg",
                "http://oud8gdknf.bkt.clouddn.com/WeChat.apk",
                "6.5.10")));

        System.out.println(getAPIString(new APP(
                "QQ",
                "腾讯推出的即时通讯工具。支持在线聊天、视频电话",
                "http://oud8gdknf.bkt.clouddn.com/qq_icon.jpg",
                "http://oud8gdknf.bkt.clouddn.com/QQ.apk",
                "7.1.5")));

        System.out.println(getAPIString(new APP(
                "News",
                "今日头条是一款基于数据挖掘的推荐引擎产品，它为用户推荐有价值的、个性化的信息，提供连接人与信息的新型服务，是国内移动互联网领域成长最快的产品服务之一",
                "http://oud8gdknf.bkt.clouddn.com/news_icon.jpg",
                "http://oud8gdknf.bkt.clouddn.com/News.apk",
                "6.2.9")));

        System.out.println(getAPIString(new APP(
                "Browser",
                "腾讯出品，精巧架构，轻快稳定",
                "http://oud8gdknf.bkt.clouddn.com/browser_icon.jpg",
                "http://oud8gdknf.bkt.clouddn.com/Browser.apk",
                "7.7.0.3440")));

    }

    private static String getAPIString(APP app) throws UnsupportedEncodingException {
        StringBuffer api = new StringBuffer("http://192.168.5.119:8080/phone/app?action=save");
        api.append("&" + NAME + "=" + app.getName());
        api.append("&" + INTRODUCTION + "=" + URLEncoder.encode(app.getIntroduction(), "UTF-8"));
        api.append("&" + ICON_URL + "=" + app.getIcon_url());
        api.append("&" + FILE_URL + "=" + app.getFile_url());
        api.append("&" + VERSION + "=" + app.getVersion());
        return api.toString();
    }

    private static void test3() {
        System.out.println(new Date(System.currentTimeMillis() - 100000));
    }

    private static void test2() throws ParameterException {
        try {
            float f = Float.parseFloat("1234r");
            System.out.println(f);
        } catch (NumberFormatException e) {
            throw new ParameterException(ParameterException.PARAMETER_FORMAT_WRONG, "values");
        }
    }

    private static void test1() {
        Session session = DBHelper.getSession();
        session.beginTransaction();
        try {
            List<User> users = session.createQuery("from User ").list();
            System.out.println(Arrays.toString(users.toArray()));
            session.getTransaction().commit();
        } catch (HibernateException e) {
            System.out.println(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }

//        List<PointHistory> pointHistoryList = pointHandle.search("from PointHistory");
//        System.out.println(Arrays.toString(pointHistoryList.toArray()));
    }
}
